#include "StorageConfig.h"
#include "config/app_config.h"

StorageConfig::StorageConfig(QWidget*parent)
    :QWidget(parent)
{
    ui.setupUi(this);
    initWidget();

    connect(ui.comboBox_flag, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.lineEdit_citycode, SIGNAL(textChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.lineEdit_carNumber, SIGNAL(textChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.spinBox_cFrequency, SIGNAL(valueChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.spinBox_bandwidth, SIGNAL(valueChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.spinBox_power, SIGNAL(valueChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.comboBox_polarization, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.spinBox_AMP_threshold, SIGNAL(valueChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.spinBox_MinPlotAreaThreshod, SIGNAL(valueChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
    connect(ui.comboBox_show_plot, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(OnAnyValueChanged(const QString &)));
}

StorageConfig::~StorageConfig()
{

}

void StorageConfig::OnAnyValueChanged(const QString &)
{
    sln_app_config::instance().set_citycode(ui.lineEdit_citycode->text().toStdString());
    sln_app_config::instance().set_car_number(ui.lineEdit_carNumber->text().toStdString());
    sln_app_config::instance().set_c_frequency(ui.spinBox_cFrequency->value());
    sln_app_config::instance().set_bandwidth(ui.spinBox_bandwidth->value());
    sln_app_config::instance().set_power(ui.spinBox_power->value());
    sln_app_config::instance().set_polarization(ui.comboBox_polarization->currentText().toStdString());
    sln_app_config::instance().set_flag(ui.comboBox_flag->currentText().toStdString());
    sln_app_config::instance().set_show_plot(ui.comboBox_show_plot->currentIndex());
    sln_app_config::instance().set_amp_threshold(ui.spinBox_AMP_threshold->value());
    sln_app_config::instance().set_area_threshold(ui.spinBox_MinPlotAreaThreshod->value());

    sln_app_config::instance().Save2File();
}

void StorageConfig::initWidget()
{
    ui.comboBox_flag->setCurrentText(QString::fromStdString(sln_app_config::instance().get_flag()));
    ui.lineEdit_citycode->setText(QString::fromStdString(sln_app_config::instance().get_citycode()));
    ui.lineEdit_carNumber->setText(QString::fromStdString(sln_app_config::instance().get_car_number()));
    ui.spinBox_cFrequency->setValue(sln_app_config::instance().get_c_frequency());
    ui.spinBox_bandwidth->setValue(sln_app_config::instance().get_bandwidth());
    ui.spinBox_power->setValue(sln_app_config::instance().get_power());
    ui.comboBox_polarization->setCurrentText(QString::fromStdString(sln_app_config::instance().get_polarization()));
    ui.spinBox_AMP_threshold->setValue(sln_app_config::instance().get_amp_threshold());
    ui.spinBox_MinPlotAreaThreshod->setValue(sln_app_config::instance().get_area_threshold());
    ui.comboBox_show_plot->setCurrentIndex(sln_app_config::instance().get_show_plot());
}